﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disk = System.IO;
using System.IO;

namespace Xzen.Images.Provider.Windows
{
    public class DiskReaderWriter :IReaderWriter
    {
        Folder rootFolder;
        string rootPath;
        public DiskReaderWriter(string root)
        {
            rootPath = root;           
        }
        private Folder ReadFolderFromDirectory(DirectoryInfo dr)
        {
            Folder folder = new Folder();
            folder.FullName = dr.FullName;
            folder.Created = dr.CreationTimeUtc;
            folder.Name = dr.Name;
            folder.Parent = dr.Parent == null ? null: ReadFolderFromDirectory(dr.Parent);
            folder.Updated = dr.LastWriteTimeUtc;
            folder.Uri = dr.FullName;
            folder.User = Environment.UserName;
            return folder;
        }
        private File ReadFileFromDirectory(FileInfo info, Folder parent)
        {
            File file = new File();
            file.FullName = info.FullName;
            file.Created = info.CreationTimeUtc;
            file.Name = info.Name;
            file.Parent = parent;// info.Directory == null ? null : ReadFolderFromDirectory(info.Directory);
            file.Updated = info.LastWriteTimeUtc;
            file.Uri = info.FullName;
            file.User = Environment.UserName;
            return file;
        }
        public Folder GetRoot()
        {
            DirectoryInfo dr = new DirectoryInfo(rootPath);
            rootFolder = ReadFolderFromDirectory(dr);
            rootFolder.Parent = null; // this means , this is root
            return rootFolder;
        }

        public List<Folder> GetFolders(Folder root)
        {
            DirectoryInfo dr = new DirectoryInfo(root.FullName);
            var dirs = (from d in dr.GetDirectories() select ReadFolderFromDirectory(d));
            return dirs.ToList();
        }

        public List<File> GetFiles(Folder parent)
        {
            DirectoryInfo dr = new DirectoryInfo(parent.FullName);
            var files = (from d in dr.GetFiles() select ReadFileFromDirectory(d, parent));
            return files.ToList();
        }

        public Folder GetFolder(string uri)
        {
            DirectoryInfo dr = new DirectoryInfo(uri);
            return ReadFolderFromDirectory(dr);
        }

        public File GetFile(string uri, bool fillWithContent = false)
        {
            FileInfo info = new FileInfo(uri);
            File file = ReadFileFromDirectory(info, ReadFolderFromDirectory(info.Directory));
            if (fillWithContent)
            {
                file.Data = Disk.File.ReadAllBytes(uri);
            }
            return file;
        }

        public File CreateFile(Folder parent, File file, bool overriteProperties = true)
        {
            string fullName = parent.FullName + "\\" + file.Name;
            if(!Disk.File.Exists(fullName))
            Disk.File.Create(fullName).Close();
            return GetFile(fullName);
        }

        public Folder CreateFolder(Folder parent, Folder child, bool overriteProperties = true)
        {
            string fullName = parent.FullName + "\\" + child.Name;
            if (!Disk.Directory.Exists(fullName))
                Disk.Directory.CreateDirectory(fullName);
            return GetFolder(fullName);
        }

        public File WriteFile(File file)
        {
            Disk.File.WriteAllBytes(file.FullName, file.Data);
            return file;
        }

        public File ReadFile(File file)
        {
            file.Data = Disk.File.ReadAllBytes(file.FullName);
            return file;
        }

        public bool IsFolderExist(Folder folder)
        {
            return Directory.Exists(folder.FullName);
        }

        public bool IsFileExist(File file)
        {
            return Disk.File.Exists(file.FullName);
        }

        public bool DeleteFile(File file)
        {
            Disk.File.Delete(file.FullName);
            return true;
        }

        public bool RemoveFolder(Folder folder)
        {
            Directory.Delete(folder.FullName);
            return true;
        }
    }
}
